module.exports = (function() {
    var express = require('express');
    var router = express.Router();
    var request = require('request');
    var querystring = require("querystring");

    router.use('/:prjId/:type/:location/:time1?/:time2?/:fuzzy?', function(req, res) {
        var url = require('./../config.json').apiUrl;
        var Base64 = require('./base64.js').Base64;

        req.params.location = Base64.decode(req.params.location, 'utf-8');

        var str = querystring.stringify(req.params);

        request({
            url: url + '/recentchanges?' + str
        }, function(error, response, body) {
            if (!error && response.statusCode == 200) {
                res.send(JSON.parse(body));
            } else {
                res.send('fail to load!');
            }
        });
    });
    return router;
})();
